package com.hmdp.config;

import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.hmdp.constants.RedisConstants.BLOOM_SHOP_KEY;

@Configuration
public class RedissonConfig {

    private final static String REDIS_URL = "127.0.0.1:6379";

    @Bean
    public RedissonClient redissonClient() {
        // 配置
        Config config = new Config();
        // userClusterServers()方法添加集群
        config.useSingleServer().setAddress("redis://" + REDIS_URL);
        //看门狗机制
        config.setLockWatchdogTimeout(30 * 1000L);
        // 创建RedissonClient对象
        return Redisson.create(config);
    }

    //TODO 配置布隆过滤器
    @Bean
    public RBloomFilter<String> bloomFilter(RedissonClient redissonClient) {
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter(BLOOM_SHOP_KEY);
        bloomFilter.tryInit(10000, 0.01);
        return bloomFilter;
    }
}